﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;

namespace HML
{
    /// <summary>
    /// 上下内边距转换器
    /// </summary>
    public class TBPaddingConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }

        /// <summary>
        /// 字符串转对象
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string valueStr = value as string;
            if (valueStr != null)
            {
                valueStr = valueStr.Trim();

                if (valueStr.Length == 0)
                {
                    return null;
                }
                else
                {
                    if (culture == null)
                    {
                        culture = CultureInfo.CurrentCulture;
                    }
                    char sep = culture.TextInfo.ListSeparator[0];
                    string[] tokens = valueStr.Split(new char[] { sep });
                    int[] values = new int[tokens.Length];
                    TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
                    for (int i = 0; i < values.Length; i++)
                    {
                        values[i] = (int)intConverter.ConvertFromString(context, culture, tokens[i]);
                    }
                    if (values.Length == 1)
                    {
                        return new TBPadding(values[0], values[0]);
                    }
                    if (values.Length == 2)
                    {
                        return new TBPadding(values[0], values[1]);
                    }
                    else
                    {
                        throw new ArgumentException("参数个数错误");
                    }
                }
            }

            return base.ConvertFrom(context, culture, value);
        }

        /// <summary>
        /// 对象转字符串
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }

            if (value is TBPadding)
            {
                if (destinationType == typeof(string))
                {
                    TBPadding rc = (TBPadding)value;

                    if (culture == null)
                    {
                        culture = CultureInfo.CurrentCulture;
                    }
                    string sep = culture.TextInfo.ListSeparator + " ";
                    TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
                    string[] args = new string[2];
                    int nArg = 0;

                    args[nArg++] = intConverter.ConvertToString(context, culture, rc.Top);
                    args[nArg++] = intConverter.ConvertToString(context, culture, rc.Bottom);

                    return string.Join(sep, args);
                }
                else if (destinationType == typeof(InstanceDescriptor))
                {
                    TBPadding rc = (TBPadding)value;

                    if (rc.ShouldSerializeAll())
                    {
                        return new InstanceDescriptor(
                            typeof(TBPadding).GetConstructor(new Type[] { typeof(int) }),
                            new object[] { rc.All });
                    }
                    else
                    {
                        return new InstanceDescriptor(
                            typeof(TBPadding).GetConstructor(new Type[] { typeof(int), typeof(int) }),
                            new object[] { rc.Top, rc.Bottom });
                    }
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (propertyValues == null)
            {
                throw new ArgumentNullException("propertyValues");
            }

            TBPadding original = (TBPadding)context.PropertyDescriptor.GetValue(context.Instance);

            int all = (int)propertyValues["All"];
            if (original.All != all)
            {
                return new TBPadding(all);
            }
            else
            {
                return new TBPadding(
                    (int)propertyValues["Top"],
                    (int)propertyValues["Bottom"]);
            }
        }

        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(TBPadding), attributes);
            return props.Sort(new string[] { "All", "Top", "Bottom" });
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}
